package com.sinaapp.gavinzhang.GSpider.webaddress;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author gavin
 * @version 1.0
 * @date 2016/3/19
 * @description 爬虫系统自带的待访问列表
 * 实现采用阻塞性双头队列
 */
public class PendingWebUrlList extends WebUrlCollection {

	/** 使用阻塞型双头队列*/
	private BlockingDeque<WebAddress> pendingList;
	public PendingWebUrlList() {
		pendingList = new LinkedBlockingDeque<WebAddress>();
	}

	public static PendingWebUrlList newPendingWebUrlList(){
		return new PendingWebUrlList();
	}

	@Override
	public boolean exist(WebAddress webUrl) {
		return pendingList.contains(webUrl);
	}

	@Override
	public WebAddress get() {
		try {
			return pendingList.take();
		} catch (InterruptedException e) {
			return null;
		}
	}

	@Override
	public WebUrlCollection add(WebAddress webUrl) {
		if(pendingList.contains(webUrl)) {
			return this;
		}
		else {
			try {
				pendingList.put(webUrl);
			} catch (InterruptedException e) {
				System.err.println("CurrentWebUrlList function add error");
				e.printStackTrace();
			}
			return this;
		}
	}

	@Override
	public WebUrlCollection remove(WebAddress webUrl) {
		if(pendingList.contains(webUrl)) {
			pendingList.remove(webUrl);
			return this;
		} else {
			return this;
		}
	}

	@Override
	public int size() {
		return pendingList.size();
	}
	@Deprecated
	public BlockingDeque<WebAddress> getBdCurrentList() {
		return pendingList;
	}
	@Deprecated
	public void setBdCurrentList(BlockingDeque<WebAddress> bdCurrentList) {
		this.pendingList = bdCurrentList;
	}

	public BlockingDeque<WebAddress> getPendingList() {
		return pendingList;
	}

	public void setPendingList(BlockingDeque<WebAddress> pendingList) {
		this.pendingList = pendingList;
	}
}
